let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isRead: 1,  //1未读，2已读
        keyword: '',
        isLoading: true,
        bindremind: undefined  //0显示绑定按钮，1不显示绑定按钮
        ,shouquan:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getComment();
    },

    /** 切换 未读、已读 */
    onRead(e){
        this.setData({ isRead: e.currentTarget.dataset.read, commentList: [], isLoading: true });
        this.getComment();
    },

    /** 搜索框输入 */
    inputSearch(e){
        this.setData({ keyword: e.detail.value});
    },

    confirmSearch(){
        
    },

    /** 获取点评消息 */
    getComment(){
        let params = {
            read: this.data.isRead,
            keyword: this.data.keyword
        };
        app.request('post', '/api/Project/commentlistApi', params).then(res => {
            let commentList = res.data.list;
            let now = new Date().getTime();
            if(!Array.isArray(commentList)) return false;
            commentList.forEach(item => {
                let sec = now - item.addtime;
                if(sec > 0 && sec <= 8.64e7){
                    let temp = new Date(item.addtime);
                    item.timeStr = `${temp.getHours()}:${temp.getMinutes() < 10 ? '0' + temp.getMinutes() : temp.getMinutes()}`;
                }
                if(sec > 8.64e7 && sec <= 8.64e7*2) item.timeStr = '昨天';
                if(sec > 8.64e7*2 && sec <= 8.64e7*3) item.timeStr = '前天';
                if(sec > 8.64e7*3) item.timeStr = '三天前';
            });


            // console.log(commentList);
            this.setData({ commentList, isLoading: false, bindremind: res.data.bindremind });
            // this.setData({ commentList, isLoading: false });
        })
        .catch(err => console.log(err));
    },

    /** 点击进入详情页 */
    onClike(e){
        wx.navigateTo({ url: `/pages/messageDetail/detail?id=${e.currentTarget.dataset.id}&name=${e.currentTarget.dataset.name}` });
    },

    /** 绑定账号 */
    getUserProfile() {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
       
        // wx.login({
        //     success: res => {
        //         if(res.errMsg == 'login:ok'){
        //             wx.getUserInfo({
        //                 success: e => {
        //                     console.log(e);
        //                 }
        //             });
        //         }
        //     }
        // });

        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (e) => {
                // console.log(res1);return;
                wx.login({
                    success: res => {
                        if(e.errMsg == 'getUserProfile:ok'){
                            let params = {
                                encryptedData: e.encryptedData,
                                iv: e.iv,
                                code: res.code,
                                appid: app.globalData.appid
                            };
                            app.request('post', '/api/Auto/program', params).then(result => {
                                // app.showSuccessToast('绑定成功');
                                this.getComment();
                                this.setData({shouquan:1})
                            }).catch(err => console.log(err));
                        }
                    }
                })
               
            }
        })


      },

      query:function(){
          this.setData({shouquan:0});
      },


    onBindAccount(){
        wx.login({
            success: res => {
                if(res.errMsg == 'login:ok'){
                    wx.getUserInfo({
                        success: e => {

                            console.log(e);return;
                            if(e.errMsg == 'getUserInfo:ok'){
                                let params = {
                                    encryptedData: e.encryptedData,
                                    iv: e.iv,
                                    code: res.code,
                                    appid: app.globalData.appid
                                };
                                app.request('post', '/api/Auto/program', params).then(result => {
                                    app.showSuccessToast('绑定成功');
                                    this.getComment();
                                }).catch(err => console.log(err));
                            }
                        }
                    });
                }
            }
        });
    },

    /** 通知授权  1xfah8tCj6m7090cMuZTfhzOHiJu_hNRydigyvWYEbo', 'ceT-0KL7fkj6GuEwJvcvhfrqqXjl-W67qq2jclgUfF0','*/
    onShouQuan(){
        let keys = ['Nfr1gM-tFOcMvGWiisr49WZn6PiCt2U3QPBSocBFaO0'];
        var that = this;
        wx.requestSubscribeMessage({
            tmplIds: keys,
            success (res) {
                if(res[keys[0]] == 'accept'){
                    that.setData({shouquan:0});
                }
            },
            fail: err => console.log(err)
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getComment();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})